package com.huizhonglm.diap.presenters.user;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.bruce.common.utils.FileUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.model.user.ChecksumAuthParams;
import com.huizhonglm.diap.api.model.user.ChecksumAuthResponse;
import com.huizhonglm.diap.api.model.user.ChecksumParams;
import com.huizhonglm.diap.api.model.user.ChecksumResponse;
import com.huizhonglm.diap.core.utils.LogUtil;
import com.huizhonglm.diap.ui.user.iView.ChecksumIView;

/**
 * @author LiXiaoLong
 * @version 1.0
 * @since 2014-11-19 11:25
 */
public class ChecksumPresenter {

    private ChecksumIView iView;

    private int encodeIndex;
    private int encodeCount;
    private String authcode;

    private String account;
    private String isRegsiter;

    public ChecksumPresenter(ChecksumIView iView) {
        this.iView = iView;
    }

    public void getChecksum(String account, String isRegsiter){
        this.account = account;
        this.isRegsiter = isRegsiter;
        getAuthor();
    }

    private void getAuthor(){
        encodeIndex = 0;
        API.http().post("getAuthor", new ChecksumAuthParams(), new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    ChecksumAuthResponse resp = JSON.parseObject(result, ChecksumAuthResponse.class);
                    if (resp != null && !EmptyUtil.isEmpty(resp.getAuthor())) {
                        encryptAuchor(resp.getAuthor());
                        getChecksum();
                    } else {
                        iView.onChecksumFailure();
                    }
                } else {
                    iView.onChecksumFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onChecksumFailure();
            }
        });
    }

    private void getChecksum(){
        ChecksumParams params = new ChecksumParams();
        params.account = account;
        params.isRegister = isRegsiter;
        params.authcode = authcode;
        API.http().post("getChecksum", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    ChecksumResponse resp = JSON.parseObject(result, ChecksumResponse.class);
                    iView.onChecksumSuccess(resp);
                } else {
                    iView.onChecksumFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onChecksumFailure();
            }
        });
    }

    private void encryptAuchor(String author){
        String last = author.substring(author.length() - 1, author.length());
        encodeCount = Integer.valueOf(last) + 1;
        LogUtil.print("getAuthor - author=" + author + " - count=" + encodeCount);
        encryption(author);
    }

    private void encryption(String author){
        while (encodeIndex < encodeCount) {
            encodeIndex ++;
            authcode = FileUtil.MD5(author);
            encryption(authcode);
        }
    }
}
